﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_FlatManger_frm_MenuBManage : System.Web.UI.Page
{
    private void BindMenuType()
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();

        DataSet dst = cls_Menu.GetSysMenuTypes();

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.chkIs_Stop.Checked = this.chkIs_Edit.Checked = false;
            this.chkIs_Stop.Enabled = this.chkIs_Edit.Enabled = this.btnEdit.Enabled = this.tbiResult.Enabled = false;
            this.txtMenuName.Enabled = this.txtMenuDesc.Enabled = this.txtMenuUrl.Enabled = false;
            this.txtMenuName.BorderColor = this.txtMenuDesc.BorderColor = this.txtMenuUrl.BorderColor = Color.FromArgb(172, 168, 153);
            this.txtMenuName.Text = "";
            this.txtMenuDesc.Text = "";
            this.txtMenuUrl.Text = "";
            this.lstBox1.Items.Clear();
            this.lstBox2.Items.Clear();
            this.dropMenuType1.Items.Clear();
            this.dropMenuType2.Items.Clear();

            this.dropMenuType1.DataTextField = this.dropMenuType2.DataTextField = "m_type_name";
            this.dropMenuType1.DataValueField = this.dropMenuType2.DataValueField = "m_type_id";

            this.dropMenuType1.DataSource = this.dropMenuType2.DataSource = dst;
            this.dropMenuType1.DataBind();
            this.dropMenuType2.DataBind();

            DataSet dstItem = cls_Menu.GetSysMenuItems(0);

            if (dstItem != null && dstItem.Tables[0].Rows.Count > 0)
            {
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetCorporationDataSet = dstItem;
                this.lstBox1.DataTextField = "m_item_name";
                this.lstBox1.DataValueField = "m_item_id";
                this.lstBox1.DataSource = dstItem;
                this.lstBox1.DataBind();
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('获取系统菜单项失败！');</script>";
        }
    }

    private void BindMenuItem(ListBox lstBox, DataSet dst)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            lstBox.Items.Clear();

            lstBox.DataTextField = "m_item_name";
            lstBox.DataValueField = "m_item_id";
            lstBox.DataSource = dst;
            lstBox.DataBind();
        }
        else
        {
            this.lblJs.Text = "<script>alert('获取系统菜单项失败！');</script>";
        }
    }

    private void DeleteMenuItem(int Command)
    { 
        int MenuTypeID=-2, MenuItemID = -1;

        if (this.lstBox1.SelectedIndex != -1)
        {
            MenuTypeID = Int32.Parse(this.dropMenuType1.SelectedItem.Value.Trim());
            MenuItemID = Int32.Parse(this.lstBox1.SelectedItem.Value.Trim());
        }
        else if (this.lstBox2.SelectedIndex != -1)
        {
            MenuTypeID = Int32.Parse(this.dropMenuType2.SelectedItem.Value.Trim());
            MenuItemID = Int32.Parse(this.lstBox2.SelectedItem.Value.Trim());
        }

        if (MenuItemID != -1 && MenuTypeID != -2)
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            Boolean is_succeed = cls_Menu.DeleteMenuItem(MenuItemID, Command);

            if (is_succeed)
            {
                DataSet dst = cls_Menu.GetSysMenuItems(MenuTypeID);
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();

                if (this.lstBox1.SelectedIndex != -1)
                {
                    cls_Property.SetOrGetCorporationDataSet = dst;

                    this.BindMenuItem(this.lstBox1, dst);
                }
                else
                {
                    cls_Property.SetOrGetChildResultDataSet = dst;

                    this.BindMenuItem(this.lstBox2, dst);
                }

                this.chkIs_Stop.Checked = this.chkIs_Edit.Checked = false;
                this.chkIs_Stop.Enabled = this.chkIs_Edit.Enabled = this.btnEdit.Enabled = this.tbiResult.Enabled = false;
                this.txtMenuName.Enabled = this.txtMenuDesc.Enabled = this.txtMenuUrl.Enabled = false;
                this.txtMenuName.BorderColor = this.txtMenuDesc.BorderColor = this.txtMenuUrl.BorderColor = Color.FromArgb(172, 168, 153);
                this.txtMenuName.Text = "";
                this.txtMenuDesc.Text = "";
                this.txtMenuUrl.Text = "";
            }
            else
            {
                this.lblJs.Text = "<script>alert('作废的菜单项失败，请重试！');</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请先选择你要作废的菜单项！');</script>";
        }
    }

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.BindMenuType();
            }
            else
            {
                this.lblJs.Text = "";
            }

            //this.tbiStop.Attributes.Add("onClick", "return confirm('您真的要停用所选的项吗？');");
            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        this.BindMenuType();
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;

        int CommandName = Int32.Parse(btn.CommandName);
        int intMenuTypeId1 = -1, intMenuTypeId2 = -1, intMenuItemID = -1;

        if (this.lstBox1.SelectedIndex != -1)
        { 
            intMenuTypeId1 = Int32.Parse(this.dropMenuType1.SelectedItem.Value.Trim());
            intMenuTypeId2 = Int32.Parse(this.dropMenuType2.SelectedItem.Value.Trim());
            intMenuItemID = Int32.Parse(this.lstBox1.SelectedItem.Value.Trim());
        }
        else if (this.lstBox2.SelectedIndex != -1)
        {
            intMenuTypeId1 = Int32.Parse(this.dropMenuType2.SelectedItem.Value.Trim());
            intMenuTypeId2 = Int32.Parse(this.dropMenuType1.SelectedItem.Value.Trim());
            intMenuItemID = Int32.Parse(this.lstBox2.SelectedItem.Value.Trim());
        }

        if (intMenuTypeId1 != -1 && intMenuItemID != -1)
        {
            int Msg = -1;

            ClsMenuDAL cls_Menu = new ClsMenuDAL();

            if (CommandName == 2)
            {
                Msg = (intMenuTypeId1 == intMenuTypeId2) ? CommandName : cls_Menu.RrmoveMenuItemByOther(intMenuTypeId1, intMenuItemID, intMenuTypeId2);
            }
            else
            {
                Msg = cls_Menu.RrmoveMenuItem(intMenuTypeId1, intMenuItemID, CommandName);
            }

            if (Msg == 0)
            {
                this.lblJs.Text = "<script>alert('该菜单项已经是第一位，不能上移！');</script>";
            }
            else if (Msg == 1)
            {
                this.lblJs.Text = "<script>alert('该菜单项已经是最后一位，不能下移！');</script>";
            }
            else if (Msg == 2)
            {
                this.lblJs.Text = "<script>alert('不能移动到相同的菜单类别中！');</script>";
            }
            else if (Msg == -1)
            {
                this.lblJs.Text = "<script>alert('操作失败，请重试！');</script>";
            }
            else
            {
                DataSet dst1 = cls_Menu.GetSysMenuItems(intMenuTypeId1);
                DataSet dst2 = cls_Menu.GetSysMenuItems(intMenuTypeId2);

                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetCorporationDataSet = dst1;
                cls_Property.SetOrGetChildResultDataSet = dst2;

                if (this.lstBox1.SelectedIndex != -1)
                {
                    this.BindMenuItem(this.lstBox1, dst1);
                    this.BindMenuItem(this.lstBox2, dst2);

                    for (int i = this.lstBox1.Items.Count - 1; i >= 0; i--)
                    {
                        if (Int32.Parse(this.lstBox1.Items[i].Value.Trim()) == intMenuItemID)
                        {
                            this.lstBox1.Items[i].Selected = true;
                            break;
                        }
                    }

                    for (int j = this.lstBox2.Items.Count - 1; j >= 0; j--)
                    {
                        if (Int32.Parse(this.lstBox2.Items[j].Value.Trim()) == intMenuItemID)
                        {
                            this.lstBox2.Items[j].Selected = true;
                            break;
                        }
                    }
                }
                else
                {
                    this.BindMenuItem(this.lstBox2, dst1);
                    this.BindMenuItem(this.lstBox1, dst2);

                    for (int i = this.lstBox2.Items.Count - 1; i >= 0; i--)
                    {
                        if (Int32.Parse(this.lstBox2.Items[i].Value.Trim()) == intMenuItemID)
                        {
                            this.lstBox2.Items[i].Selected = true;
                            break;
                        }
                    }

                    for (int j = this.lstBox1.Items.Count - 1; j >= 0; j--)
                    {
                        if (Int32.Parse(this.lstBox1.Items[j].Value.Trim()) == intMenuItemID)
                        {
                            this.lstBox1.Items[j].Selected = true;
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请选择您要移动的菜单项！');</script>";
        }
    }   

    protected void dropMenuType1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.lstBox2.SelectedIndex == -1)
        {
            this.chkIs_Stop.Checked = false;
            this.txtMenuName.Text = "";
            this.txtMenuDesc.Text = "";
            this.txtMenuUrl.Text = "";
        }

        int intID = Int32.Parse(this.dropMenuType1.SelectedItem.Value.Trim());

        ClsMenuDAL cls_Menu = new ClsMenuDAL();

        DataSet dst = cls_Menu.GetSysMenuItems(intID);
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        cls_Property.SetOrGetCorporationDataSet = dst;

        this.BindMenuItem(this.lstBox1, dst);
    }

    protected void dropMenuType2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.lstBox1.SelectedIndex == -1)
        {
            this.chkIs_Stop.Checked = false;
            this.txtMenuName.Text = "";
            this.txtMenuDesc.Text = "";
            this.txtMenuUrl.Text = "";
        }

        int intID = Int32.Parse(this.dropMenuType2.SelectedItem.Value.Trim());

        ClsMenuDAL cls_Menu = new ClsMenuDAL();

        DataSet dst = cls_Menu.GetSysMenuItems(intID);
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        cls_Property.SetOrGetChildResultDataSet = dst;

        this.BindMenuItem(this.lstBox2, dst);
    }

    protected void lstBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.chkIs_Edit.Enabled = true;
        this.chkIs_Stop.Checked = false;
        this.txtMenuName.Text = "";
        this.txtMenuDesc.Text = "";
        this.txtMenuUrl.Text = "";

        this.lstBox2.SelectedIndex = -1;

        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intItems = dst.Tables[0].Rows.Count - 1;
            int MenuID = Int32.Parse(this.lstBox1.SelectedItem.Value.Trim());

            for (int i = intItems; i >= 0; i--)
            {
                if (MenuID == Int32.Parse(dst.Tables[0].Rows[i][0].ToString()))
                {
                    this.chkIs_Stop.Checked = !bool.Parse(dst.Tables[0].Rows[i][6].ToString());
                    this.txtMenuName.Text = dst.Tables[0].Rows[i][2].ToString();
                    this.txtMenuDesc.Text = dst.Tables[0].Rows[i][3].ToString();
                    this.txtMenuUrl.Text = dst.Tables[0].Rows[i][4].ToString();

                    break;
                }
            }
        }
    }

    protected void lstBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.chkIs_Edit.Enabled = true;
        this.chkIs_Stop.Checked = false;
        this.txtMenuName.Text = "";
        this.txtMenuDesc.Text = "";
        this.txtMenuUrl.Text = "";

        this.lstBox1.SelectedIndex = -1;

        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetChildResultDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intItems = dst.Tables[0].Rows.Count - 1;
            int MenuID = Int32.Parse(this.lstBox2.SelectedItem.Value.Trim());

            for (int i = intItems; i >= 0; i--)
            {
                if (MenuID == Int32.Parse(dst.Tables[0].Rows[i][0].ToString()))
                {
                    this.chkIs_Stop.Checked = !bool.Parse(dst.Tables[0].Rows[i][6].ToString());
                    this.txtMenuName.Text = dst.Tables[0].Rows[i][2].ToString();
                    this.txtMenuDesc.Text = dst.Tables[0].Rows[i][3].ToString();
                    this.txtMenuUrl.Text = dst.Tables[0].Rows[i][4].ToString();

                    string strUrl = dst.Tables[0].Rows[i][4].ToString().Trim();

                    if (strUrl == "-1" || strUrl == "-2" || strUrl == "-3")
                    {
                        this.txtMenuUrl.Enabled = false;
                    }
                    else
                    {
                        this.txtMenuUrl.Enabled = true;
                    }

                    break;
                }
            }
        }
    }

    protected void chkIs_Edit_CheckedChanged(object sender, EventArgs e)
    {
        if (this.lstBox1.SelectedIndex != -1 || this.lstBox2.SelectedIndex != -1)
        {
            if (((CheckBox)sender).Checked)
            {
                this.chkIs_Stop.Enabled = this.btnEdit.Enabled = this.tbiResult.Enabled = true;
                this.txtMenuName.Enabled = this.txtMenuDesc.Enabled = this.txtMenuUrl.Enabled = true;
                this.txtMenuName.BorderColor = this.txtMenuDesc.BorderColor = this.txtMenuUrl.BorderColor = Color.FromArgb(127, 157, 185);
            }
            else
            {
                this.chkIs_Stop.Enabled = this.btnEdit.Enabled = this.tbiResult.Enabled = false;
                this.txtMenuName.Enabled = this.txtMenuDesc.Enabled = this.txtMenuUrl.Enabled = false;
                this.txtMenuName.BorderColor = this.txtMenuDesc.BorderColor = this.txtMenuUrl.BorderColor = Color.FromArgb(172, 168, 153);
            }
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        int MenuItemID = -1;

        if (this.lstBox1.SelectedIndex != -1)
        {
            MenuItemID = Int32.Parse(this.lstBox1.SelectedItem.Value.Trim());
        }
        else if (this.lstBox2.SelectedIndex != -1)
        {
            MenuItemID = Int32.Parse(this.lstBox2.SelectedItem.Value.Trim());
        }

        if (MenuItemID != -1)
        {
            string strMenuName = this.txtMenuName.Text.Trim();

            if (strMenuName != "")
            {
                int is_Stop = (this.chkIs_Stop.Checked) ? 0 : 1;
                string strMenuDesc = this.txtMenuDesc.Text.Trim();
                string strMenuUrl = this.txtMenuUrl.Text.Trim();

                ClsMenuDAL cls_Menu = new ClsMenuDAL();
                Boolean is_succeed = cls_Menu.UpdateMenuItem(MenuItemID, is_Stop, strMenuName, strMenuDesc, strMenuUrl);

                if (is_succeed)
                {
                    this.lblJs.Text = "<script>alert('修改系统菜单项成功！');</script>";
                }
                else
                {
                    this.lblJs.Text = "<script>alert('修改系统菜单项失败，请重试！');</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('菜单名称不能为空！');</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请先选择你要编辑的菜单项！')</script>";
        }
    }

    protected void tbiResult_Click(object sender, EventArgs e)
    {
        int MenuID = -1;
        DataSet dst = null;
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        if (this.lstBox1.SelectedIndex != -1)
        {
            dst = cls_Property.SetOrGetCorporationDataSet;
            MenuID = Int32.Parse(this.lstBox1.SelectedItem.Value.Trim());
        }
        else if (this.lstBox2.SelectedIndex != -1)
        {
            dst = cls_Property.SetOrGetChildResultDataSet;
            MenuID = Int32.Parse(this.lstBox2.SelectedItem.Value.Trim());
        }

        if (dst != null && dst.Tables[0].Rows.Count > 0 && MenuID != -1)
        {
            int intItems = dst.Tables[0].Rows.Count - 1;

            for (int i = intItems; i >= 0; i--)
            {
                if (MenuID == Int32.Parse(dst.Tables[0].Rows[i][0].ToString()))
                {
                    this.chkIs_Stop.Checked = !bool.Parse(dst.Tables[0].Rows[i][6].ToString());
                    this.txtMenuName.Text = dst.Tables[0].Rows[i][2].ToString();
                    this.txtMenuDesc.Text = dst.Tables[0].Rows[i][3].ToString();
                    this.txtMenuUrl.Text = dst.Tables[0].Rows[i][4].ToString();

                    break;
                }
            }
        }
        else
        {
            this.chkIs_Stop.Checked = false;
            this.txtMenuName.Text = "";
            this.txtMenuDesc.Text = "";
            this.txtMenuUrl.Text = "";
        }
    }

    protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        int CommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteMenuItem(CommandName);
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int CommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteMenuItem(CommandName);
    }
}
